// pages/waybill/waybill.js
var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navindex: 0,
    According: false,
    freightnav: [
      { txt: '我的发布' ,val:3},
      { txt: '待装货' ,val:5},
      { txt: '待卸货',val:6 },
      { txt: '待支付' ,val:-1},
      { txt: '已完成' ,val:4},
      
    ],
    // multiArray:['余额支付','微信支付'],
    waybill: [
      // {
      //   num: '20180905135059', state: '推送', weight: '33', price: '400.00', ads1: '西安', ads2: '北京市'
      // }, {
      //   num: '20180905135059', state: '我的报价', weight: '33', price: '400.00', ads1: '西安', ads2: '北京市'
      // }, {
      //   num: '20180905135059', state: '待卸货', weight: '33', price: '400.00', ads1: '西安', ads2: '北京市'
      // }, {
      //   num: '20180905135059', state: '待装货', weight: '33', price: '400.00', ads1: '西安', ads2: '北京市'
      // },
    ],
    currentPage: 1,
    totalPage: 1,
    type: 3,
  },
  applyWey:function(e){
    var id = e.currentTarget.dataset.tid;
    app.navTo('../defaultaplTrade/defaultapl?id=' + id)
  },
  lookStatus: function (e) {
    var id = e.currentTarget.dataset.tid;
    app.navTo('../wbillstate/wbillstate?id=' + id)
  },
  wyDeal:function(e){
    var id = e.currentTarget.dataset.oid;
    app.navTo('../wydeal/queren_zhuanghuo?id=' + id)
   
  },
  cancelTrade:function(e){
    var id = e.currentTarget.dataset.tid;
    var that = this;
    var param = {
      id:id
    };

    wx.showModal({
      title: '温馨提示:',
      content: '您确定取消运单吗?',
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          wx.showLoading();
          util.commonRequest('cancelTrade', 'post', param, function (res) {

            wx.hideLoading();
            var data = res.data;
            if (data.code == 200) {
              that.setData({
                currentpage: 1,
                waybill: []
              })
              that.initData();

            } 
            // else if (data.code == 111) {
            //   app.navTo('../cancelTrade/queren_zhuanghuo?id=' + id);

            // } 
            else {
              wx.showToast({
                icon: 'loading',
                title: data.msg,
              })
            }
          });
        }
      },
    })
  },

  querenzhuanghuo: function (e) {
    var id = e.currentTarget.dataset.oid;
    var formId = e.detail.formId;
    this.collectFromId(formId);
    app.navTo('../queren_zhuanghuo/queren_zhuanghuo?id='+id+"&type=1");
 
  },
  collectFromId: function (formid) {
    var that = this;
    var param = {
      form_id: formid
    };
    util.commonRequest('formId', 'post', param, function (res) {

    });
  },
  //货主确认卸货
  querenxiehuo: function (e) {
    var id = e.currentTarget.dataset.oid;
    var formId = e.detail.formId;
    this.collectFromId(formId);
    app.navTo('../queren_zhuanghuo/queren_zhuanghuo?id=' + id + "&type=2");
 
  },
  goPay:function(e){
    var id = e.currentTarget.dataset.oid;
    var formId = e.detail.formId;
    this.collectFromId(formId);
    app.navTo("../to_pay/to_pay?id="+id);

  },
 
  initData: function () {
    var that = this;
    var param = {
      currentpage: that.data.currentPage,
      status: that.data.type
    };
    wx.showLoading();
    // util.commonRequest('newOrderList', 'post', param, function (res) {
    util.commonRequest('myPublish', 'post', param, function (res) {
      // debugger
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        var tempArr = data.data.data;
        var oriArr = that.data.waybill;
        var finalyArr = oriArr.concat(tempArr);
        that.setData({
          waybill: finalyArr,
          totalPage: data.data.last_page
        })
        if (finalyArr.length > 0) {
          that.setData({
            According: false
          })
        } else {
          that.setData({
            According: true
          })
        }
        // 
      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  //运单状态
  wbillstate(e) {
    var id = e.currentTarget.dataset.id;
    app.navTo('../wbillstate/wbillstate?id=' + id)
  },
  //运单申请
  defaultapl(e) {
    var id = e.currentTarget.dataset.id;
    app.navTo('../defaultapl/defaultapl?id=' + id)
  },
  //完成卸货
  completeXh: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '温馨提示:',
      content: '您确定卸货完毕吗?',
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          that.xhFun(id);
        }
      },
    })
  },
  xhFun: function (id) {

    var that = this;
    var param = {
      id: id
    };
    wx.showLoading();
    util.commonRequest('confirmUnloadnt', 'post', param, function (res) {
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
        that.setData({
          currentpage: 1,
          waybill: []
        })
        that.initData();

      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  confirmCom: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '温馨提示:',
      content: '您确定装货完毕吗?',
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          that.zhFun(id);
        }
      },
    })
  },
  //确认完成
  zhFun: function (id) {

    var that = this;
    var param = {
      id: id
    };
    wx.showLoading();
    util.commonRequest('confirmShipment', 'post', param, function (res) {
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
        that.setData({
          currentpage: 1,
          waybill: []
        })
        that.initData();

      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  //运单详情
  waybilldeil(e) {
    var id = e.currentTarget.dataset.id;
    app.navTo('../waybilldeil/waybilldeil?id=' + id)
  },

  click: function (e) {
    var id = e.currentTarget.dataset.id;
    var val = e.currentTarget.dataset.val;
    this.setData({
      navindex: id,
      type: val,
      waybill: [],
      currentpage: 1

    })
    this.initData();
  }, 
  detail: function (e) {
    var tid = e.currentTarget.dataset.tid;
    if (this.data.type == 3){
      app.navTo('../shipper_waybilldeil/waybilldeil?tid=' + tid);
    }else{
      app.navTo('../waybilldeil/waybilldeil?id=' + tid)
    }
  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    var type = options.type;
    if (type) {
       if(type == 5){
         this.setData({ type: type, navindex: 1 })
       }else if (type == 6) {
        this.setData({ type: type, navindex: 2 })
       }else if (type == -1) {
         this.setData({ type: type, navindex: 3 })
       }
       else{
        this.setData({ type: type, navindex: type })
       }
     

    }
    // this.initData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    that.setData({
      waybill: [],
      currentpage: 1
      })
    that.initData();

    var HQgoods = that.data.waybill;
    if (HQgoods.length == 0) {
      that.setData({
        According: true,
      })
    } else {
      that.setData({
        According: false,
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      currentpage: 1,
      waybill: []
    })
    setTimeout(() => {
      wx.stopPullDownRefresh()
      this.initData();
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var currentpage = this.data.currentPage;
    var totalpage = this.data.totalPage;
    if (currentpage < totalpage) {
      this.setData({
        currentPage: currentpage + 1
      })
      this.initData();
    } else {
      console.log("已经是全部数据")
      // wx.showToast({
      //   'icon':'loading',
      //   title: '已经是全部数据了',
      // })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})